using System;
using System.Collections.Generic;
using UnityEngine;

namespace NodeCanvas.DialogueTrees
{
	[AddComponentMenu("")]
	public abstract class DLGNodeBase : Node, ITaskSystem, IAutoSortable
	{
		[SerializeField]
		private string _actorName = "_Owner";

		public override string nodeName
		{
			get
			{
				return string.Empty;
			}
		}

		public override int maxInConnections
		{
			get
			{
				return -1;
			}
		}

		public override int maxOutConnections
		{
			get
			{
				return 1;
			}
		}

		public override Type outConnectionType
		{
			get
			{
				return typeof(Connection);
			}
		}

		private string actorName
		{
			get
			{
				return _actorName;
			}
			set
			{
				_actorName = value;
				DLGTree.actorReferences[value] = DialogueActor.FindActorWithName(value);
				Task[] componentsInChildren = GetComponentsInChildren<Task>(true);
				foreach (Task task in componentsInChildren)
				{
					task.SetOwnerSystem(this);
				}
			}
		}

		protected DialogueTree DLGTree
		{
			get
			{
				return (DialogueTree)base.graph;
			}
		}

		private List<string> actorNames
		{
			get
			{
				List<string> list = new List<string>(DLGTree.dialogueActorNames);
				list.Insert(0, "_Owner");
				return list;
			}
		}

		protected string finalActorName
		{
			get
			{
				if (!actorNames.Contains(actorName))
				{
					return "<color=#d63e3e>*" + actorName + "*</color>";
				}
				return actorName;
			}
		}

		protected DialogueActor finalActor
		{
			get
			{
				if (actorName == "_Owner" || string.IsNullOrEmpty(actorName))
				{
					return (!(base.graphAgent == null)) ? base.graphAgent.GetComponent<DialogueActor>() : null;
				}
				if (!DLGTree.actorReferences.ContainsKey(actorName))
				{
					DLGTree.actorReferences[actorName] = DialogueActor.FindActorWithName(actorName);
				}
				return DLGTree.actorReferences[actorName];
			}
		}

		protected Blackboard finalBlackboard
		{
			get
			{
				return (!(finalActor == null)) ? finalActor.blackboard : null;
			}
		}

		public Component agent
		{
			get
			{
				return finalActor;
			}
		}

		public Blackboard blackboard
		{
			get
			{
				return finalBlackboard;
			}
		}

		public float elapsedTime
		{
			get
			{
				return base.graph.elapsedTime;
			}
		}

		public void SendTaskOwnerDefaults()
		{
			Task[] componentsInChildren = GetComponentsInChildren<Task>(true);
			foreach (Task task in componentsInChildren)
			{
				task.SetOwnerSystem(this);
			}
		}

		protected void Continue()
		{
			base.status = Status.Success;
			if (DLGTree.isRunning)
			{
				if (base.outConnections.Count == 0)
				{
					DLGTree.StopGraph();
				}
				else
				{
					base.outConnections[0].Execute(finalActor, finalBlackboard);
				}
			}
		}
	}
}
